package model;

import java.util.HashMap;

class Registers extends FunctionalUnit {
	
	private HashMap<Integer, Integer> content = new HashMap<Integer, Integer>();
	
	private Pipe wbPipe;
	
	private int base;
	
	public Registers(Pipe sourcePipe, Pipe destinationPipe, Pipe wbPipe) {
		super(sourcePipe, destinationPipe);
		this.wbPipe = wbPipe;
	}
	
	public void setData(int address, int data) {
		content.put(address, data);
	}
	
	public int getData(int address) {
		return content.get(address);
	}
	
	public void execute1() {
		base = getPipeData("BASE");
		Facade.getInstance().move(UnitEnum.Pipe1, UnitEnum.Registers, base);
		
		int destreg = wbPipe.getData("DESTREG");
		int data = wbPipe.getData("DATA");
		if (destreg != 0) {
			content.put(destreg, data); //modificat conditia != 0	
			Facade.getInstance().move(UnitEnum.Pipe4, UnitEnum.Registers, destreg, data);
		}
	}	
	
	public void execute2() {
		setPipeData("BASE", content.get(base));
		Facade.getInstance().move(UnitEnum.Registers, UnitEnum.Pipe2, base, content.get(base) );
	}

}
